package co.yixiang.modules.shop.rest;

import co.yixiang.aop.log.Log;
import co.yixiang.modules.shop.domain.ProductBrand;
import co.yixiang.modules.shop.service.ProductBrandService;
import co.yixiang.modules.shop.service.dto.ProductBrandQueryCriteria;
import org.apache.commons.beanutils.ConvertUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

/**
* @author lijunze
* @date 2020-04-28
*/
@Api(tags = "配件商品品牌管理")
@RestController
@RequestMapping("/api/productBrand")
public class ProductBrandController {

    private final ProductBrandService productBrandService;

    public ProductBrandController(ProductBrandService productBrandService) {
        this.productBrandService = productBrandService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('productBrand:list')")
    public void download(HttpServletResponse response, ProductBrandQueryCriteria criteria) throws IOException {
        productBrandService.download(productBrandService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询配件商品品牌")
    @ApiOperation("查询配件商品品牌")
    public ResponseEntity<Object> getProductBrands(ProductBrandQueryCriteria criteria, Pageable pageable){

        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("message", productBrandService.queryAll(criteria,pageable));
        stringObjectHashMap.put("code",200);
        return ResponseEntity.ok(stringObjectHashMap);
    }

    @PostMapping
    @Log("新增配件商品品牌")
    @ApiOperation("新增配件商品品牌")
    @PreAuthorize("@el.check('productBrand:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody ProductBrand resources){
        resources.setIsDel("0");
//        Timestamp ts = new Timestamp(System.currentTimeMillis());
//        int time = (int) ((ts.getTime())/1000);;
        resources.setCreateTime(LocalDateTime.now());
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("message", productBrandService.create(resources));
        stringObjectHashMap.put("code",200);
        return ResponseEntity.ok(stringObjectHashMap);

    }

    @PutMapping
    @Log("修改配件商品品牌")
    @ApiOperation("修改配件商品品牌")
    @PreAuthorize("@el.check('productBrand:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody ProductBrand resources){
        productBrandService.update(resources);
        Map map = new HashMap();
        map.put("code",200);

        return ResponseEntity.ok(map);


    }

    @Log("删除配件商品品牌")
    @ApiOperation("删除配件商品品牌数组")
    @PreAuthorize("@el.check('productBrand:del')")
    @DeleteMapping("/delete/arr")
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        productBrandService.deleteAll(ids);
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("code",200);
        return ResponseEntity.ok(stringObjectHashMap);
    }




    @Log("删除配件商品品牌")
    @ApiOperation("删除配件商品品牌")
    @PreAuthorize("@el.check('userShop:del')")
    @RequestMapping(value = "/{ids}", method = RequestMethod.DELETE)
    public ResponseEntity<Object> delete(@PathVariable String ids) {
        String [] id = ids.split(",");
        Integer[] aftIdArray = (Integer[]) ConvertUtils.convert(id, Integer.class);
        productBrandService.deleteAll(aftIdArray);
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("code",200);
        return ResponseEntity.ok(stringObjectHashMap);

    }
}